/**
 * 
 */
package org.isaaclabs.mymdb;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * Provides APIs to perform basic HTTP operations
 * @author hemanth.sunkara
 * @version $Revision: 1.0 $
 */
public class HttpUtils {
    /**
     * Field httpclient.
     */
    private static HttpClient httpclient;

    /**
     * Initialized the client and makes it usable
     */
    public static void initialize() {
        httpclient = new DefaultHttpClient();
    }

    /**
     * Hits a given URL and returns the response string
     * @param url String
     * @return String
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String hitURL(String url) 
    throws ClientProtocolException, IOException {
        HttpGet httpget = new HttpGet(url);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpget, responseHandler);
        return responseBody;
    }

    /**
     * Shuts down the client connection manager.
     * Client should be reinitialized to be usable again.
     */
    public static void shutdown() {
        httpclient.getConnectionManager().shutdown();
    }

}
